﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Business;
using hou_elearning.Entity;

public partial class Controls_ucDangky : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lbtnOK_Click(object sender, EventArgs e)
    {
        try
        {
            NguoidungEntity oNguoidung = new NguoidungEntity();
            oNguoidung.sHoten = txtHoten.Text;
            oNguoidung.sTendangnhap = txtTendangnhap.Text;
            oNguoidung.sMatkhau =  txtMatkhau.Text;
            oNguoidung.sSodienthoai = txtSodienthoai.Text;
            oNguoidung.sEmail = txtEmail.Text;
            oNguoidung.sDiachi = txtDiachi.Text;
            try
            {
                oNguoidung.dNgaysinh = DateTime.Parse(txtNgaysinh.Text);
            }
            catch { }
            oNguoidung.bGioitinh = Boolean.Parse(ddlGioitinh.SelectedValue);
            if (imgAnhthe.HasFile)
            {
                oNguoidung.bAnhthe = CreateImgByte();
            }
            Int32 PK_iNguoidungID = PK_iNguoidungID = NguoidungBRL.Add(oNguoidung);
            if (PK_iNguoidungID > 0)
            {
                /* Lưu thông tin vào Session */
                Session["UserID"] = oNguoidung.PK_iNguoidungID;
                Session["Username"] = oNguoidung.sTendangnhap;
                Session["FullName"] = oNguoidung.sHoten;
                /* Lưu thông tin vào Cookie */
                HttpCookie userInfor = new HttpCookie("HOU_eLearning_userInfor");
                userInfor["strUsername"] = oNguoidung.sTendangnhap;
                userInfor["UserPass"] = oNguoidung.sMatkhau;
                userInfor.Expires = DateTime.Now.AddDays(7); //Nhớ 1 tuần
                Response.Cookies.Add(userInfor);
                Response.Redirect(ResolveUrl(ResolveUrl("~/Admin")));
            }
        }
        catch (Exception ex)
        {
            lblThongbao.Text = ex.Message;
        }
    }
    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl("~/"));
    }

    public Byte[] CreateImgByte()
    {
        Byte[] imgByte = null;
        try
        {
            if (imgAnhthe.HasFile && imgAnhthe.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = imgAnhthe.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
                return imgByte;
            }
        }
        catch
        {
            lblThongbao.Text = "Không tải được logo!";
        }
        return imgByte;
    }
}